package com.conncui.common.emoji;

import com.conncui.common.tool.LifeCycle;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by cui on 16-10-28.
 */

public class Encode {

    /**
     * ??[-16, -97, -104, -126]
     * \ud83d\ude02
     * 602
     */
    private static String TAG = "Encode";

    public static String stringToUnicode(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            if (ch > 255)
                str += "\\u" + Integer.toHexString(ch);
            else
                str += "\\" + Integer.toHexString(ch);
        }
        System.out.println(TAG + "," + LifeCycle.getCurrentMethodName() + "," + str);
        return str;
    }

    /*

 *  ?????Unicode?????????????

 */

    //format like \\udb08\\u4fu9
    public static String utf16ToString(String str) {

        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");
        }
        System.out.println(TAG + "," + LifeCycle.getCurrentMethodName() + "," + str);
        return str;

    }


    public static String unicodeToString(String str) {

        /**
         like below , 1f483-1f3fb is not mean code between 1f483 and 1f3fb
         actually,it mean high-low point
         "dancer_tone1": "unicode": "1f483-1f3fb",

         "runner_tone1": "unicode": "1f3c3-1f3fb",
         "runner_tone2": "unicode": "1f3c3-1f3fc",
         "runner_tone3": "unicode": "1f3c3-1f3fd",

         "bath_tone1":   "unicode": "1f6c0-1f3fb",


         There are four type code

         11-03 13:57:39.166 D/Line    (19464): afaffd8  emojiOne:1f491
         11-03 13:57:41.813 D/Line    (19464): fd17371  emojiOne:1f934-1f3fe
         11-03 13:57:39.169 D/Line    (19464): fe2ef31  emojiOne:1f469-2764-1f469
         11-03 13:57:39.175 D/Line    (19464): 50c4184  emojiOne:1f469-2764-1f48b-1f469
         * */

        StringBuilder charArrSB = new StringBuilder();
        StringBuilder info = new StringBuilder();
        try {
            String[] codeArr = str.split("-");
            System.out.println(TAG + "," + "unicodeToString:" + "," + str + "," + Integer.toString(codeArr.length));
            for (int i = 0; i < codeArr.length; i++) {
                String code = codeArr[i];
                int codeDec = Integer.parseInt(code, 16);
                info.append("code:").append(code).append("D:").append("H:").append(codeDec);
                char[] charArr = Character.toChars(codeDec);
                for (int j = 0; j < charArr.length; j++) {
                    charArrSB.append(charArr[j]);
                    info.append("-").append(charArr[j]);
                }
                System.out.println(TAG + "," + info.toString());
            }
        } catch (Exception e) {
            System.out.println(TAG + "," + e.toString());
        }
        System.out.println(TAG + "," + "unicode:" + "," + str + "," + "string:" + "," + charArrSB.toString());
        return charArrSB.toString();

    }
}
